package controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import systems.MailSystem;

import models.WebmailModel;
import models.WebsiteModel;
import entities.Webmail;
import entities.Website;

@SuppressWarnings("serial")
public class WebmailSendServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		WebmailModel webmailModel = WebmailModel.getInstance();
		Webmail webmail  = webmailModel.get(id);
		
		if (webmail != null && webmail.getStatus() == 0 || webmail.getStatus() == 3) {
			webmail.setStatus(1);
			
			if (MailSystem.getId().isEmpty() || MailSystem.getStatus() == 1) {
				webmail.setStatus(1);
				//Reset MailSystem
				MailSystem.reset();
				
				//Set new MailSystem
				MailSystem.setId(webmail.getId());
				MailSystem.setStatus(0);
			}
			
			webmailModel.insert(webmail);
		}
		
		response.sendRedirect("/webmail/list");
		return;
	}
}
